<?php

namespace app\wechat\controller;

use app\wechat\validate\AddOrderValidate;
use think\Db;

/**
 * Class Apply
 * @package app\wechat\controller
 */
class Apply extends Base
{
    /**
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */

    public function NewOrder()
    {
        $user     = session("user");
        $userInfo = model('users')->find($user['id']);
        if (request()->isAjax()) {
            $data = input('post.');

//            $ret  = $this->validate($data, AddOrderValidate::class);
//            if ($ret !== true) {
//                $this->error($ret);
//            }
//
//            $OrderInfo = DB::name('order')->order('id', 'desc')->limit(1)->find();
//            $date      = substr($OrderInfo['order_number'], 0, 8);
//            $num       = substr($OrderInfo['order_number'], 8, 3);
//            $today     = date('Ymd');
//            if ($today > $date) {
//                $data['order_number'] = $today . str_pad(1, 3, '0', STR_PAD_LEFT);
//            } else {
//                $data['order_number'] = $date . str_pad((int)$num + 1, 3, '0', STR_PAD_LEFT);
//            }
            $data['user_id']     = $user['id'];
            $data['date']        = date('Y-m-d');
            if(Db::name('order')->where("date",$data['date'])->where("user_id",$user['id'])->find()){
                $this->error('每天只能提交一次');
            }
//            $data['send_time']   = strtotime($data['send_time']);
//            $data['status']      = 1;
//            $data['create_time'] = time();
//            $data['apply_time']  = time();
//            $data['apply_name']  = $userInfo['real_name'];
//            $wechatMessage       = new Message();
//            $all_user = model('users')->select();
//            foreach ($all_user as $user_one) {
//                if (strstr($user_one['role'], '2')) {
//                    $wechatMessage->ToExpress($user_one['openid'], $userInfo['real_name'], $data['goods']);
//                }
//            }
            $result = model('order')->save($data);
            if ($result) {
                $this->success('提交成功', 'wechat/index/index');
            } else {
                $this->error('提交失败');
            }
        }
        $viewData = [
            'user' => $userInfo
        ];
        $this->assign($viewData);
        return view();
    }

    //已经申请的单据

    /**
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function orderList()
    {
        $user       = session("user");
        $user_order = model('order')
            ->where('user_id', $user['id'])
            ->order('id', 'desc')
            ->select();
        $viewData   = [
            'order' => $user_order
        ];
        $this->assign($viewData);
        return view();
    }

    public function ApplyInformation()
    {
        return view();
    }

    public function SelectList()
    {
        $orderID   = input('id');
        $priceInfo = model('price')->where('order_num', $orderID)->select();
        $viewData  = [
            'price' => $priceInfo
        ];
        $this->assign($viewData);
        return view();
    }
}
